package com.gilight.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gilight.base.base.ResultEntity;
import com.gilight.base.dto.ColdProductDto;
import com.gilight.base.dto.ColdProductParameterDto;
import com.gilight.base.dto.ColdProductParameterRuleDto;
import com.gilight.base.dto.ColdProductQueryDto;
import com.gilight.base.model.ColdProduct;
import com.gilight.base.model.SysUser;
import com.gilight.base.service.IColdProductService;
import com.gilight.base.service.SysUserZzjgService;
import com.gilight.base.util.ShiroUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author shr
 * @since 2021-07-15
 */
@RestController
@RequestMapping("/web/coldProduct")
public class ColdProductController {

    @Autowired
    private IColdProductService coldProductServiceImpl;
    @Autowired
    private SysUserZzjgService sysUserZzjgService;
    @ApiOperation(value = "根据id查询商品详情")
    @RequestMapping(value = "/selectById", method = {RequestMethod.GET})
    public ResultEntity selectById(@RequestParam(value = "productId") Long productId) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(coldProductServiceImpl.getProduct(productId));
        return resultEntity;
    }

    @ApiOperation(value = "条件查询")
    @RequestMapping(value = "/getProductList", method = {RequestMethod.GET})
    public ResultEntity getProductList(ColdProductQueryDto coldProductQueryDto) {
        ResultEntity resultEntity = new ResultEntity();
        resultEntity.setData(coldProductServiceImpl.getProductList(coldProductQueryDto));
        return resultEntity;
    }

    @ApiOperation(value = "分页条件查询")
    @RequestMapping(value = "/page", method = {RequestMethod.GET})
    public ResultEntity page(ColdProductQueryDto coldProductQueryDto,
                             @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        ResultEntity resultEntity = new ResultEntity();
        QueryWrapper<ColdProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(coldProductQueryDto.getZzjgId()!=null,"zzjgId",coldProductQueryDto.getZzjgId())
                    .eq(coldProductQueryDto.getStatus()!=null,"status",coldProductQueryDto.getStatus())
                    .eq(coldProductQueryDto.getProductId()!=null,"id",coldProductQueryDto.getProductId())
                    .like(StringUtils.isNotBlank(coldProductQueryDto.getProductName()),"name",coldProductQueryDto.getProductName());
        Page page = new Page(pageNo,pageSize);
        resultEntity.setData(coldProductServiceImpl.page(page,queryWrapper));
        return resultEntity;
    }

    @ApiOperation(value = "添加商品")
    @RequestMapping(value = "/saveProduct", method = {RequestMethod.POST})
    public ResultEntity saveProduct(@RequestBody ColdProductDto coldProductDto) {
        ResultEntity resultEntity = new ResultEntity();
        coldProductServiceImpl.saveProduct(coldProductDto);
        return resultEntity;
    }

    @ApiOperation(value = "设置规格互斥规则")
    @RequestMapping(value = "/saveParameterRule", method = {RequestMethod.POST})
    public ResultEntity saveParameterRule(@RequestBody List<ColdProductParameterRuleDto> list) {
        ResultEntity resultEntity = new ResultEntity();
        coldProductServiceImpl.saveParameterRule(list);
        return resultEntity;
    }

    @ApiOperation(value = "更新商品信息")
    @RequestMapping(value = "/updateById", method = {RequestMethod.POST})
    public ResultEntity updateById(@RequestBody ColdProductDto coldProductDto) {
        ResultEntity resultEntity = new ResultEntity();
        coldProductServiceImpl.updateProduct(coldProductDto);
        return resultEntity;
    }

    @ApiOperation(value = "更新商品规格信息")
    @RequestMapping(value = "/updateParameterById", method = {RequestMethod.POST})
    public ResultEntity updateParameter(@RequestBody ColdProductDto coldProductDto) {
        ResultEntity resultEntity = new ResultEntity();
        coldProductServiceImpl.updateParameter(coldProductDto.getProductId(),coldProductDto.getColdProductParameter(),coldProductDto.getZzjgId());
        return resultEntity;
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/removeById", method = {RequestMethod.POST})
    public ResultEntity removeById(@RequestBody ColdProduct coldProduct) {
        ResultEntity resultEntity = new ResultEntity();
        coldProductServiceImpl.removeById(coldProduct.getId());
        return resultEntity;
    }

}
